<!--
 * @Author: hua
 * @Date: 2020-07-21 15:21:54
 * @description: 
 * @LastEditors: hua
 * @LastEditTime: 2020-07-23 10:20:12
--> 
<template>
  <!--内容-->
  <div class="container">
    <h2>{{$t('autoExam')}}:</h2>
    <el-row>
      <el-button @click="handleOpenLock">{{$t('openLock')}}</el-button>
      <el-button type="primary" @click="handleDidi">{{$t('Didi')}}</el-button>
      <el-button type="success" @click="handleSleep">{{$t('sleep')}}</el-button>
      <el-button type="warning" @click="handleTest">{{$t('autoExam')}}</el-button>
    </el-row>
    <el-divider></el-divider>
    <h2>{{$t('EquipmentExamScreen')}}:</h2>
    <div style="display:flex;justify-content:center;flex-direction:column;align-items: center;">
      <el-steps :active="activeStep" align-center finish-status="success">
        <el-step :title="$t('step')+'1'" :description="$t('beginning')"></el-step>
        <el-step :title="$t('step')+'2'" :description="$t('one')"></el-step>
        <el-step :title="$t('step')+'3'" :description="$t('two')"></el-step>
        <el-step :title="$t('step')+'4'" :description="$t('three')"></el-step>
        <el-step :title="$t('step')+'5'" :description="$t('four')"></el-step>
        <el-step :title="$t('step')+'6'" :description="$t('ending')"></el-step>
      </el-steps>
    </div>
    <el-divider v-if="chooseShow"></el-divider>
    <h2 v-if="chooseShow">{{$t('successOrFail')}}:</h2>
    <div
      v-if="chooseShow"
      style="display:flex;flex-direction:row;justify-content:center;width:100%;margin-top:30px;"
    >
      <el-button
        type="success"
        icon="el-icon-check"
        circle
        style="margin-right:60px;zoom:2"
        @click="handleSaveTestLog('1')"
      ></el-button>
      <el-button
        type="danger"
        icon="el-icon-close"
        circle
        style="margin-left:60px;zoom:2"
        @click="handleSaveTestLog('2')"
      ></el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { Message, MessageBox } from "element-ui";
import { isvalidUsername } from "@/utils/validate";
import { sendCommand, getLock } from "@/api/lock";
export default {
  data() {
    return {
      simcardImei: "",
      activeStep: 1,
      chooseShow: false,
    };
  },
  computed: {
    ...mapGetters(["userInfo", "productionBatch", "equipmentInfo"]),
  },
  mounted() {
    if (!this.equipmentInfo.simcardImei) {
      MessageBox.confirm(this.$t("imeiError"), this.$t("error"), {
        confirmButtonText: this.$t("button.ok"),
        cancelButtonText: this.$t("button.cancel"),
        type: "warning",
      }).then(() => {
        this.$router.push({ name: "behindCode" });
      });
      return;
    }
    this.simcardImei = this.equipmentInfo.simcardImei;
  },
  methods: {
    /* 开锁 */
    handleOpenLock() {
      sendCommand(this.userInfo.id, "OL", this.simcardImei).then((res) => {
        Message({
          message: this.$t("sendSuccess"),
          type: "success",
          duration: 5 * 1000,
        });
      });
    },
    /* 滴滴 */
    handleDidi() {
      sendCommand(this.serInfo.id, "DD", this.simcardImei).then((res) => {
        Message({
          message: this.$t("sendSuccess"),
          type: "success",
          duration: 5 * 1000,
        });
      });
    },
    /* 休眠 */
    handleSleep() {
      sendCommand(this.userInfo.id, "SLEEP", this.simcardImei).then((res) => {
        Message({
          message: this.$t("sendSuccess"),
          type: "success",
          duration: 5 * 1000,
        });
      });
    },
    handleTest() {
      this.activeStep = 2;
      setTimeout(() => {
        setTimeout(() => {
          this.activeStep = 3;
          this.activeStep = 4;
          setTimeout(() => {
            this.activeStep = 5;
            this.activeStep = 6;
            this.chooseShow = true;
          }, 3000);
        }, 3000);
      }, 3000);
    },
    /* 整锁测试 */
    handleEntireLockTesting() {
      console.log(this.userInfo.id, "DD", this.simcardImei);
      sendCommand(this.userInfo.id, "DD", this.simcardImei)
        .then((res) => {
          this.activeStep = 2;
          setTimeout(() => {
            let time = 0;
            //三秒检测一次锁是否关闭
            let isCloseFirst = setInterval(() => {
              time++;
              if (time > 7) {
                clearInterval(isCloseFirst);
                time = 0;
                MessageBox.confirm("检测错误", "锁状态检测超过限制次数7", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                }).then(() => {
                  this.handleSaveTestLog("2");
                });
              }
              getLock(this.userInfo.id, this.simcardImei, "").then((res) => {
                if (res.body.lockStatus) {
                  //清除循环检测
                  clearInterval(isCloseFirst);
                  /* setKnockShellBoxShow(true);
                  setLockOpenShow(true); */
                  this.activeStep = 3;
                  this.activeStep = 4;
                  setTimeout(() => {
                    let timea = 0;
                    //三秒检测一次锁是否开启
                    let isClose = setInterval(() => {
                      timea++;
                      if (timea > 7) {
                        clearInterval(isClose);
                        timea = 0;
                        MessageBox.confirm(
                          "检测错误",
                          "锁状态检测超过限制次数7",
                          {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                          }
                        ).then(() => {
                          this.handleSaveTestLog("2");
                        });
                      }
                      getLock(this.userInfo.id, this.simcardImei, "").then(
                        (res) => {
                          if (res.body.lockStatus === 0) {
                            //清除循环检测
                            clearInterval(isClose);
                            setTimeout(() => {
                              console.log(
                                this.userInfo.id,
                                "SLEEP",
                                this.simcardImei
                              );
                              sendCommand(
                                this.userInfo.id,
                                "SLEEP",
                                this.simcardImei
                              ).then((res) => {
                                this.activeStep = 5;
                                this.activeStep = 6;
                                this.chooseShow = true;
                              });
                            }, 3000);
                          }
                        }
                      );
                    }, 3000);
                  }, 3000);
                }
              });
            }, 3000);
            //setShowChoice(true);
          }, 3000);
        })
        .catch((e) => {});
    },
    /* 电池盒测试流程 */
    handleTesting() {
      console.log(this.userInfo.id, "DD", this.simcardImei);
      sendCommand(this.userInfo.id, "DD", this.simcardImei)
        .then((res) => {
          setDdShow(true);
          setTimeout(() => {
            let timeb = 0;
            //三秒检测一次锁是否关闭
            let isCloseFirst = setInterval(() => {
              timeb++;
              if (timeb > 7) {
                clearInterval(isCloseFirst);
                timeb = 0;
                MessageBox.confirm("检测错误", "锁状态检测超过限制次数7", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                }).then(() => {
                  this.handleSaveTestLog("2");
                });
              }
              getLock(this.userInfo.id, this.simcardImei, "").then((res) => {
                if (res.body.lockStatus) {
                  //清除循环检测
                  clearInterval(isCloseFirst);
                  setKnockShellBoxShow(true);
                  setLockOpenShow(true);
                  setTimeout(() => {
                    let timec = 0;
                    //三秒检测一次锁是否关闭
                    let isClose = setInterval(() => {
                      timec++;
                      if (timec > 7) {
                        clearInterval(isClose);
                        timec = 0;
                        MessageBox.confirm(
                          "检测错误",
                          "锁状态检测超过限制次数7",
                          {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                          }
                        ).then(() => {
                          this.handleSaveTestLog("2");
                        });
                      }
                      getLock(this.userInfo.id, this.simcardImei, "").then(
                        (res) => {
                          if (res.body.lockStatus) {
                            //清除循环检测
                            clearInterval(isClose);

                            setEndShow(true);
                            setShowChoice(true);
                          }
                        }
                      );
                    }, 3000);
                  }, 3000);
                }
              });
            }, 3000);
          }, 3000);
        })
        .catch((e) => {});
    },
    /* 保存测试日志 */
    handleSaveTestLog(result) {
      if (!this.productionBatch.id) {
        MessageBox.confirm(this.$t("productionBatchError"), this.$t("error"), {
          confirmButtonText: this.$t("button.ok"),
          cancelButtonText: this.$t("button.cancel"),
          type: "warning",
        }).then(() => {
          this.$router.push({ name: "chooseProductionPlan" });
        });
        return;
      }
      console.log(
        this.productionBatch.id,
        this.userInfo.id,
        null,
        result,
        this.simcardImei
      );
      saveTestLog(
        this.productionBatch.id,
        this.userInfo.id,
        null,
        result,
        this.simcardImei
      ).then((res) => {
        Message({
          message: this.$t("logSuccess"),
          type: "success",
          duration: 5 * 1000,
        });
      });
    },
  },
};
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.container {
  height: 100%;
  width: 100%;
  display: flex;
  padding: 20px;
  /*  justify-content: center;
  align-items: center; */
  flex-direction: column;
}
.modify_password_form {
  width: 80%;
}
.reverse {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}
@media (max-width: 500px) {
  .left_container {
    flex: 65px 0 0;
  }
}
</style>